package com.example.petplatform.service;

import com.example.petplatform.dao.PetDao;
import com.example.petplatform.dao.petSurrenderDao;
import com.example.petplatform.entity.Pet;
import com.example.petplatform.entity.petSurrender;
import com.example.petplatform.entity.Param.Params;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class petSurrenderService {
    @Resource
    private petSurrenderDao petSurrenderDao;
    @Resource
    private PetDao petDao;

    public List<petSurrender> getAllSurrenders() {
        return petSurrenderDao.getAllSurrenders();
    }

    public PageInfo<petSurrender> surrenderSearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<petSurrender> list = petSurrenderDao.surrenderSearch(params);
        return PageInfo.of(list);
    }

    public List<petSurrender> surrenderAdd(petSurrender surrender) {
        petSurrenderDao.insertSelective(surrender);
        return petSurrenderDao.getAllSurrenders();
    }

    public List<petSurrender> surrenderUpdate(petSurrender surrender) {
        petSurrenderDao.updateByPrimaryKeySelective(surrender);
        if ("approved".equals(surrender.getStatus())) {
            Pet pet = new Pet();
            pet.setPet_name(surrender.getPet_name());
            pet.setAge(surrender.getPet_age());
            pet.setPet_character(surrender.getPet_description());
            pet.setGender(surrender.getPet_gender());
            pet.setImage(surrender.getPet_image());
            pet.setPet_type_id(surrender.getPet_type_id());
            petDao.insertSelective(pet);
        }
        return petSurrenderDao.getAllSurrenders();
    }

    public List<petSurrender> surrenderDelete(Integer id) {
        petSurrenderDao.deleteByPrimaryKey(id);
        return petSurrenderDao.getAllSurrenders();
    }
}
